package com.leijie.grpc;

import com.leijie.proto.MyRequest;
import com.leijie.proto.MyResponse;
import com.leijie.proto.StreamRequest;
import com.leijie.proto.StreamResponse;
import io.grpc.stub.StreamObserver;

import java.util.UUID;

/**
 * @author LeiJie
 * @time 2020-5-7 13:45
 * @desc TODO
 */
public class PersionServiceImpl extends com.leijie.proto.PersionServiceGrpc.PersionServiceImplBase {

    @Override
    public void getRealNameByUsername(MyRequest request, StreamObserver<MyResponse> responseObserver) {
        System.out.println("接收到客户端信息：>>>"+request.getUsername());
        responseObserver.onNext(com.leijie.proto.MyResponse.newBuilder().setRealname("张三").build());
        responseObserver.onCompleted();
    }

    @Override
    public void getPersionByAge(StreamRequest request, StreamObserver<StreamResponse> responseObserver) {
        System.out.println("收到客户端信息：>>>"+request.getAge());

        responseObserver.onNext(com.leijie.proto.StreamResponse.newBuilder().setName("张三").setAge(10).setCity("北京").build());
        responseObserver.onNext(com.leijie.proto.StreamResponse.newBuilder().setName("李四").setAge(20).setCity("上海").build());
        responseObserver.onNext(com.leijie.proto.StreamResponse.newBuilder().setName("王五").setAge(30).setCity("武汉").build());
        responseObserver.onNext(com.leijie.proto.StreamResponse.newBuilder().setName("赵六").setAge(40).setCity("成都").build());
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<StreamRequest> getPersionWrapperByAges(StreamObserver<com.leijie.proto.ListResponse> responseObserver) {
        return new StreamObserver<StreamRequest>() {
            @Override
            public void onNext(StreamRequest value) {
                System.out.println("收到客户端数据：>>>"+value.getAge());
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                System.out.println(">>>>>>>服务端返回数据<<<<<<<<<");
                StreamResponse response = StreamResponse.newBuilder().setName("张三").setAge(100).setCity("河南").build();
                StreamResponse response2 = StreamResponse.newBuilder().setName("李四").setAge(200).setCity("河北").build();

                com.leijie.proto.ListResponse listResponse = com.leijie.proto.ListResponse.newBuilder().addStreamResponse(response).addStreamResponse(response2).build();
                responseObserver.onNext(listResponse);
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<com.leijie.proto.StrRequest> biTalk(StreamObserver<com.leijie.proto.StrResponse> responseObserver) {
        return new StreamObserver<com.leijie.proto.StrRequest>() {
            @Override
            public void onNext(com.leijie.proto.StrRequest value) {
                System.out.println("收到客户端请求数据：>>>"+value.getRequestInfo());

                com.leijie.proto.StrResponse response = com.leijie.proto.StrResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build();
                responseObserver.onNext(response);
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                System.out.println(">>>>>>>>服务端Completed!<<<<<<<<<<<");
                responseObserver.onCompleted();
            }
        };
    }
}
